package com.ProdigiousAT;

import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class KDF {

    @Test
    public void myDriver() throws Exception{
        String[][] xTC, xTS;
        String vKW, vXPath, vTestData, vError;
        WebDriver myD = new FirefoxDriver();

        // Read the TC and TS
        xTC = DDF.readXL("C:/Users/Worker/Desktop/kdfFiles/KDFAutomation.xls", "Test Cases");
        xTS = DDF.readXL("C:/Users/Worker/Desktop/kdfFiles/KDFAutomation.xls", "Test Steps");

        for (int i=1; i<xTC.length ; i++) {// Go to each Row in TC
            if (xTC[i][3].equalsIgnoreCase("Y")) { //identify which TC to execute
                for (int k=1;k<xTS.length; k++){// Go to every TS
                    if (xTC[i][0].equalsIgnoreCase(xTS[k][1])) { // see if TCID's match

                        vKW = xTS[k][4];
                        vXPath = xTS[k][5];
                        vTestData = xTS[k][6];
                        vError = "Pass";

                        try{
                            // Call the corresponding Keyword Function
                            if (vKW.equalsIgnoreCase("enterText")) {
                                KW.enterText(myD, vXPath, vTestData);
                            }
                            if (vKW.equalsIgnoreCase("navigateBrowser")) {
                                KW.navigateBrowser(myD,vTestData);
                            }
                            if (vKW.equalsIgnoreCase("clickElement")) {
                                KW.clickElement(myD,vXPath);
                            }
                            if (vKW.equalsIgnoreCase("readText")) {
                                KW.readText(myD,vXPath);
                                System.out.println("Text here is '" + KW.readText(myD,vXPath)+"'");
                            }
                            if (vKW.equalsIgnoreCase("verifyText")) {
                                vError = KW.verifyText(myD,vXPath, vTestData);
                                System.out.println("Test step (verifyText) is a " + vError);
                            }
                            if (vKW.equalsIgnoreCase("waitTime")) {
                                KW.waitTime(4000);
                            }
                            if (vError.equalsIgnoreCase("Pass")) {
                                System.out.println("Pass: " + xTC[i][0] + "_" + vKW);
                                xTS[k][7] = "Pass";
                            } else {
                                System.out.println("Fail: " + xTC[i][0] + "_" + vKW);
                                xTS[k][7] = "Fail";
                                xTS[k][8] = "Error happened as function returned Fail";
                            }

                        }
                        catch(Exception a){
                            System.out.println("Fail: " + xTC[i][0] + "_" + vKW);
                            xTS[k][7] = "Fail";
                            System.out.println("Error happened " + a);
                            xTS[k][8] = "Error happened " + a;
                            xTC[i][4] = "Fail";
                        }
                        // Capture o/p results
                    }
                }
            }

        }
        myD.close();
        myD.quit();

        DDF.writeXL("C:/Users/Worker/Desktop/kdfFiles/KDFAutomation-RESULTS.xls", "Results",xTS);
    }
}
